package com.ele.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ele.pojo.Business;
import com.ele.pojo.ResponseData;
import com.ele.service.BusinessService;
import com.ele.mapper.BusinessMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
* @author 28444
* @description 针对表【business】的数据库操作Service实现
* @createDate 2024-04-08 17:23:41
*/
@Service
public class BusinessServiceImpl extends ServiceImpl<BusinessMapper, Business>
    implements BusinessService{
    @Resource
    private BusinessMapper businessMapper;
    @Override
    public ResponseData<Business> getBusinessList(Integer currentPage, Integer pageSize){
        // 封装查询的结果数据
        ResponseData<Business> responseData = new ResponseData<>();
        // 条件查询
        LambdaQueryWrapper<Business> wrapper = new LambdaQueryWrapper<>();
        // 店铺处于营业状态
        wrapper.eq(Business::getStatus, 2)
                .eq(Business::getRecommend, 1);
        // 书写分页参数
        Page<Business> page = new Page<>(currentPage, pageSize);
        this.businessMapper.selectPage(page, wrapper);
        // 分页和条件查询数据
        if (page.getRecords().size() > 0){
            responseData.setCode(20000);
            responseData.setTotal(page.getTotal());
            responseData.setDatas(page.getRecords());
        }else{
            responseData.setCode(50000);
            responseData.setMessage("没有查询到数据");
        }
        return responseData;
    }

    @Override
    public ResponseData<Business> getBusinessInfoList(Integer currentPage, Integer pageSize) {
        ResponseData<Business> responseData = new ResponseData<>();
        LambdaQueryWrapper<Business> wrapper = new LambdaQueryWrapper<>();
        // 条件为店铺必须是营业状态
        wrapper.eq(Business::getStatus,2);
        Page<Business> page = new Page<>(currentPage,pageSize);
        // 直接在这里使用Mybatisplus的service层的分页查询，这时不会主动查询总记录数
        List<Business> Businesss = this.list(page, wrapper);
        // 还需要继续调用service层的方法，查询总记录数
        long count = this.count(wrapper);
        if(Businesss != null && Businesss.size() > 0){
            responseData.setCode(20000);
            responseData.setTotal(count);
            responseData.setDatas(Businesss);
        }else{
            responseData.setCode(50000);
            responseData.setMessage("没有查询到数据");
        }
        return responseData;
    }
}